﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BLL;
using Models;

namespace Workflow.Bug
{
    public partial class Detail : System.Web.UI.Page
    {
        private BugManager objBugManager = new BugManager();
        private UserTeamRoleManager objUserTeamRoleManager = new UserTeamRoleManager();
        int workParentId;
        protected void Page_Load(object sender, EventArgs e)
        {
            workParentId = Convert.ToInt32(Request.QueryString["WorkParentId"]);
            if (!IsPostBack)
            {
                try
                {
                    BugClass objBug = objBugManager.GetBugInfo(workParentId);
                    if (objBug != null)
                    {
                        ddlPName.SelectedValue = objBug.PName;
                        ddlMName.SelectedValue = objBug.MName;
                        ddlQuestion.SelectedValue = objBug.QuestionType;
                        txtDes.Text = objBug.QuestionDescription;
                        string comDate = objBug.CompleteDay.ToString("yyyy-MM-dd");
                        if(comDate!="0001-01-01")
                        {
                            string str = string.Format("<script>document.getElementById('txtCompleteTime').value = '{0}';</script>", comDate);
                            Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
                        }
                        txtMoldPerson.Text = objBug.MoldPerson;
                        txtPlan.Text = objBug.PlanDescription;
                    }
                    else
                    {
                        ddlPName.SelectedIndex = 0;
                        ddlMName.SelectedIndex = 0;
                        ddlQuestion.SelectedIndex = 0;
                        txtDes.Text = "";
                        //txtComplete.Text = "";
                        txtMoldPerson.Text = "";
                        txtPlan.Text = "";
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                }
            }
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            BugClass objBug = new BugClass
            {
                WorkParentId = workParentId,
                PName = ddlPName.SelectedValue,
                MName = ddlMName.SelectedValue,
                QuestionType = ddlQuestion.SelectedValue,
                QuestionDescription = txtDes.Text.ToString().Trim(),
                MoldPerson = txtMoldPerson.Text.ToString().Trim(),
                PlanDescription = txtPlan.Text.ToString().Trim()
            };
            if (Request.Params.Get("txtCompleteTime")==null)
            {
                objBug.CompleteDay=Convert.ToDateTime(Request.Params.Get("0001-01-01"));
            }
            else
            {
                objBug.CompleteDay=Convert.ToDateTime(Request.Params.Get("txtCompleteTime"));
            }
            try
            {
                objBugManager.InsertBug(objBug);
                string comDate = objBug.CompleteDay.ToString("yyyy-MM-dd");
                string str = string.Format("<script>document.getElementById('txtCompleteTime').value = '{0}';</script>", comDate);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
                string msg = string.Format("<script>alert('保存成功！');window.opener=null;window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);


            }
            catch (Exception ex)
            {
                
                string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }
    }
}